package dice.sinanya.db.monsterlist;

import dice.sinanya.db.tools.DbUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import static dice.sinanya.system.NpcListCache.MONSTER_LIST;
import static dice.sinanya.tools.makedata.MakeProperties.makeProStrToMap;

/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 查询log开启状况，包含当前群内是否有log开启，是哪个log，目前某个log是否处于开启状态
 */
public class SelectMonsterList {
    private static final Logger log = LogManager.getLogger(SelectMonsterList.class.getName());


    public SelectMonsterList() {
        //        初始化时无需逻辑
    }

    /**
     * 刷新日志标记位对应的开关值到静态变量中
     */
    public void selectMonsterListFromDatabase(String groupId) {
        MONSTER_LIST.remove(groupId);
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select name,properties from test.monsterList where groupId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, groupId);
                try (ResultSet set = ps.executeQuery()) {
                    HashMap<String, HashMap<String, Integer>> TMP_MONSTER = new HashMap<>();
                    while (set.next()) {
                        String name = set.getString("name");
                        String properties = set.getString("properties");
                        TMP_MONSTER.put(name, makeProStrToMap(properties));
                    }
                    MONSTER_LIST.put(groupId, TMP_MONSTER);
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }
}
